package com.campus.help;

import com.campus.help.entity.Task;
import com.campus.help.entity.User;
import com.campus.help.enums.TaskStatus;
import com.campus.help.repository.TaskRepository;
import com.campus.help.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.time.LocalDateTime;

@Component
public class CampusHelpDataInitializer {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private TaskRepository taskRepository;

    @PostConstruct
    public void init() {
        // 初始化示例用户（先检查是否存在）
        addUserIfNotExists("2023001", "123456", "张三", "13800000001", "zhangsan@example.com");
        addUserIfNotExists("2023002", "123456", "李四", "13800000002", "lisi@example.com");
        addUserIfNotExists("2023003", "123456", "王五", "13800000003", "wangwu@example.com");
        addUserIfNotExists("2023005", "123456", "赵六", "13800000005", "zhaoliu@example.com");
        addUserIfNotExists("2023006", "123456", "钱七", "13800000006", "qianqi@example.com");
        addUserIfNotExists("2023007", "123456", "孙八", "13800000007", "sunba@example.com");
        addUserIfNotExists("2023008", "123456", "周九", "13800000008", "zhoujiu@example.com");
        addUserIfNotExists("2023009", "123456", "吴十", "13800000009", "wushi@example.com");
        addUserIfNotExists("2023010", "123456", "郑十一", "13800000010", "zhengshiyi@example.com");
        addUserIfNotExists("2023011", "123456", "冯十二", "13800000011", "fengshier@example.com");

        // 初始化示例任务（先检查是否存在）
        addTaskIfNotExists(
                "TASK001",
                "帮忙取快递",
                "帮忙到校门口快递站取一个小型包裹，送到学生公寓3号楼502室",
                LocalDateTime.now().plusDays(1),
                new BigDecimal("10.00"),
                TaskStatus.PENDING.name(),
                "2023001",
                null,
                LocalDateTime.now(),
                LocalDateTime.now()
        );
        addTaskIfNotExists(
                "TASK002",
                "寻找共享单车",
                "帮忙在校园内找一辆可用的共享单车，并将其停放在图书馆门口",
                LocalDateTime.now().plusHours(12),
                new BigDecimal("5.00"),
                TaskStatus.ACCEPTED.name(),
                "2023002",
                "2023005",
                LocalDateTime.now().minusHours(2),
                LocalDateTime.now().minusHours(1)
        );
        addTaskIfNotExists(
                "TASK003",
                "笔记整理",
                "帮忙整理高等数学课程的笔记，要求字迹工整，内容完整",
                LocalDateTime.now().plusDays(3),
                new BigDecimal("50.00"),
                TaskStatus.COMPLETED.name(),
                "2023003",
                "2023006",
                LocalDateTime.now().minusDays(5),
                LocalDateTime.now().minusDays(1)
        );
        addTaskIfNotExists(
                "TASK004",
                "食堂代购",
                "帮忙在第一食堂购买两份套餐，送到计算机学院实验室",
                LocalDateTime.now().plusHours(3),
                new BigDecimal("15.00"),
                TaskStatus.PENDING.name(),
                "2023007",
                null,
                LocalDateTime.now().minusMinutes(30),
                LocalDateTime.now().minusMinutes(30)
        );
        addTaskIfNotExists(
                "TASK005",
                "图书馆占座",
                "明天上午8点在图书馆三楼自习区帮忙占两个相邻的座位",
                LocalDateTime.now().plusHours(18),
                new BigDecimal("20.00"),
                TaskStatus.PENDING.name(),
                "2023008",
                null,
                LocalDateTime.now().minusHours(4),
                LocalDateTime.now().minusHours(4)
        );
        addTaskIfNotExists(
                "TASK006",
                "课程PPT分享",
                "寻找计算机网络课程的完整PPT资料，需包含本学期所有章节",
                LocalDateTime.now().plusDays(5),
                new BigDecimal("30.00"),
                TaskStatus.ACCEPTED.name(),
                "2023009",
                "2023010",
                LocalDateTime.now().minusDays(3),
                LocalDateTime.now().minusDays(2)
        );
        addTaskIfNotExists(
                "TASK007",
                "活动海报设计",
                "为学生会举办的校园歌手大赛设计宣传海报，需要在两天内完成",
                LocalDateTime.now().plusDays(2),
                new BigDecimal("100.00"),
                TaskStatus.PENDING.name(),
                "2023011",
                null,
                LocalDateTime.now().minusHours(12),
                LocalDateTime.now().minusHours(12)
        );
    }

    private void addUserIfNotExists(String studentId, String password, String name, String phone, String email) {
        if (!userRepository.existsByStudentId(studentId)) {
            userRepository.save(new User(studentId, password, name, phone, email));
        }
    }

    private void addTaskIfNotExists(String taskId, String title, String description, LocalDateTime deadline, BigDecimal reward, String status, String publisherId, String acceptorId, LocalDateTime createTime, LocalDateTime updateTime) {
        if (!taskRepository.findByTaskId(taskId).isPresent()) {
            taskRepository.save(new Task(taskId, title, description, deadline, reward, status, publisherId, acceptorId, createTime, updateTime));
        }
    }
} 